<?php

session_start();

/* Include the config file */
require_once('db.php');
require_once('config.php');

/* include the twitter OAuth library files */
require_once('lib/twitterOAuth.php');
require_once('lib/OAuth.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
  <title>oo_tweet_tools: save recent tweets</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8;" />
  
</head>
<body>

<?php
    /*
    Create a new TwitterOAuth object, and then
    get a request token. The request token will be used
    to build the link the user will use to authorize the
    application. 

     You should probably use a try/catch here to handle errors gracefully
    */
if ((!isset($_SESSION['oauth_access_token'])) || ($_SESSION['oauth_access_token'])=='') {
    $to = new TwitterOAuth($consumer_key, $consumer_secret);
    $tok = $to->getRequestToken();

    $request_link = $to->getAuthorizeURL($tok);

    $_SESSION['oauth_request_token'] = $token = $tok['oauth_token'];
    $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];

    echo '<h1>Error: Login plz.</h1>';
    echo '<div><a href="'.$request_link.'">login using twitter</a> | ';
} else {
    $to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);

    echo '<ol>';
    $i = 0;
for ($page = 1; $page <= 20; $page++)
{
	echo '<h4>page: '.$page.'</h4>';
    $content = $to->OAuthRequest('https://api.twitter.com/1/statuses/user_timeline.json?include_rts=t&count=200&page='.$page, 'GET', array());
    $status = json_decode($content,TRUE);

    $db = new XE_DB;

    foreach ($status as $tweet) {
	$query = 'select * from '.$table.' where id="'.$tweet['id'].'";';
	$db->execute_query($query);
	if ($db->get_num_rows()) {
	    echo '<li>'.$tweet['id'].' tweet had saved already: '.$db->get_num_rows().'</li>';
	    continue;
	}

	$others = '';
	foreach($tweet as $key => $value)
	{
	    if(!empty($value) && !is_array($value))
	    {
		switch($key) {
		
		case id:
		case created_at:
		    break;
		case source:
		    $source = addslashes($value);
		    break;
		case text:
		    $text = addslashes($value);
		    break;
		default:
		    $others = $others.'<'.$key.'>'.$value.'</'.$key.'>';
		    break;
		}
	    }
	}

	$query = "INSERT INTO `".$table."` (id,created_at,source,text,others) VALUES ('".$tweet['id']."','".$tweet['created_at']."','".$source."','".$text."','".$others."')";
	//$query = "UPDATE `".$table."` SET `id` = '".$tweet['id']."',`created_at` = '".$tweet['created_at']."',`source`='".$source."',`text`='".$text."',`others`='".$others."' WHERE id='".$tweet['id']."'";
	$res = $db->execute_update($query);
	if ($res > 0) {
	    $i++;
	    $res_msg = 'OK';
	}
	else {
	    $res_msg = 'Failed';
	}
	echo '<li><strong>'.$tweet['id'].'</strong> has inserted: '.$res_msg.'</li>';
	//echo '[DEBUG] query = '.$query.'<br/>';
    }
}
    echo '</ol>';
    echo '<div>Completed: '.$i.' tweets has saved | ';
    $db->close();
}

echo '<a href="view_tweets.php">back</a></div>';

?>

<?php
/* for Debug
foreach ($_SESSION as $key => $value) {
    echo $key.' = '.$value.'<br/>';
}
echo '<pre>';
print_r($status);
echo '</pre>';

*/
?>
<div id="footer">
<address>powered by <a href='#'>oo_tweet for PHP</a></address>
</div>

</body>
</html>
